/*
 * @Author: lwHao
 * @Date: 2020-07-14 19:50:49
 * @LastEditors: lwHao
 * @LastEditTime: 2020-07-15 14:01:55
 */ 
module.exports = class MyPlugin {
  apply(compiler){
    console.log('MyPlugin 启动');
    compiler.hooks.emit.tap('MyPlugin', compilation => {
      // compliation => 可以理解为此次打包的上下文
      for (const name in compilation.assets){
        // console.log(compilation.assets[name].source());
        if (name.endsWith('.js')) {
          const contents = compilation.assets[name].source()
          const withoutComments = contents.replace(/\/\*\*+\*\//g, '')
          compilation.assets[name] = {
            source: () => withoutComments,
            size: () => withoutComments.length
          }
        }

      }
    })
  }
}